#include <iostream>
#include <fcntl.h>
#include <memory>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
#include "InetAddr.hpp"
#include "Socket.hpp"
#include "Log.hpp"
using namespace SocketModule;
using namespace LogModule;
const static int SIZE = sizeof(fd_set) * 8;
const static int defaultfd = -1;
class SelectServer {
public:
    SelectServer(int port) : _listensock(std::make_unique<TcpSocket>()), _isrunning(false) {
        for(int i = 0; i < SIZE; i++) {
            _fd_array[i] = defaultfd;
        }
        _fd_array[0] = _listensock->Fd();
    }
    void Start() {
        _isrunning = true;
        while (_isrunning) {
            fd_set fset;
            FD_ZERO(&fset);
            int maxfd = defaultfd;
            for(int i = 0; i < SIZE; i++) {
                if(_fd_array[i] == defaultfd) {
                    continue;
                }
                FD_SET(_fd_array[i], &fset);
                if(maxfd < _fd_array[i]) {
                    maxfd = _fd_array[i];
                }
            }
            PrintFd();
            int n = select(maxfd + 1, &fset, nullptr, nullptr, nullptr);
            if(n == -1) {
                LOG(LogLevel::ERROR) << "select error";
            }else if(n == 0) {
                LOG(LogLevel::INFO) << "time out";
            }else {
                LOG(LogLevel::INFO) << "有事件就绪了";
                Dispathcher(fset);
            }
        }
        _isrunning = false;
    }
    void Dispathcher(fd_set& fd) {
        for(int i = 0; i < SIZE; i++) {
            if(_fd_array[i] == defaultfd) continue;
            if(FD_ISSET(_fd_array[i], &fd)) {
                if(_fd_array[i] == _listensock->Fd()) {
                    Accepter();
                }else {
                    Recver(_fd_array[i], i);
                }
            }
        }
    }
    void Accepter() {
        InetAddr client;
        int sockfd = _listensock->Accept(&client);
        if(sockfd >= 0) {
            LOG(LogLevel::FATAL) << "获取连接成功, sockfd :" << sockfd << "client: " << client.StringAddr(); 
        }
        int i = 0;
        for(; i < SIZE; i++) {
            if(_fd_array[i] == defaultfd) {
                break;
            }
        }
        if(i == SIZE) {
            LOG(LogLevel::FATAL) << "select server full";
            close(sockfd);
            return;
        }
        _fd_array[i] = sockfd;
    }
    void Recver(int fd, int pos) {
        char buffer[1024];
        while (1) {
            int n = recv(fd, buffer, sizeof(buffer), 0);
            if(n < 0) {
                LOG(LogLevel::FATAL) << "recv error";
                _fd_array[pos] = defaultfd;
                close(fd);
            }else if(n == 0) {
                LOG(LogLevel::INFO) << "client quie";
                _fd_array[pos] = defaultfd;
                close(fd);
            }else {
                buffer[n] = '\0';
                std::cout << "client say# " << buffer << std::endl;
            }
        }
    }
    void PrintFd() {
        std::cout << "_fd_array[]: ";
        for(int i = 0; i < SIZE; i++) {
            if(_fd_array[i] == defaultfd) continue;
            std::cout << _fd_array[i] << " ";
        }
        std::cout << endl;
    }
    void Stop() {
        _isrunning = false;
    }
    ~SelectServer() {

    }
private:
    std::unique_ptr<Socket> _listensock;
    bool _isrunning;
    int _fd_array[SIZE];
};